﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Tests
//  File name:	TestNotifications.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Smart.Common;
using Smart.Common.Entities;
using Smart.Common.Notifications;

namespace Smart.Documents.Tests
{
	[TestClass]
	public class TestNotifications
	{
		[TestMethod]
		public void Notifications()
		{
			const string TestTemplateID = "Test Template";
			INotificationScope entityScope = null;
			IEntityFacade<NotificationTemplate> notificationTemplateFacade = null;
			IEntityFacade<NotificationItem> notificationInstanceFacade = null;

			bool mailInstanceAdded = false, mailTemplateAdded = false, mailFormatted = false;

			var res = SPH.RunUnder("http://srv-kirsanova/sites/rosinter/", delegate
																			{
																				entityScope = SPServices.GetScope<INotificationScope>(Smart.Common.Const.ScopeNames.Notifications);

																				notificationTemplateFacade = entityScope.Templates;
																				notificationInstanceFacade = entityScope.Queue;

																				//clear lists
																				foreach (var m in notificationInstanceFacade.All) m.Recycle();
																				foreach (var t in notificationTemplateFacade.All) t.Recycle();

																				var mail = notificationInstanceFacade.Create();

																				mail.Address = "i.solomatov@mail.ru";
																				mail.Body = "Direct Added Body";
																				mail.Subject = "Direct Added Body";
																				mail.SendAt = DateTime.Now;
																				mail.RetriesNo = 0;

																				mailInstanceAdded = mail.Save();

																				var template = notificationTemplateFacade.Create();

																				template.Title = TestTemplateID;
																				template.Body = "##param1## Test email ##param## ##link## ##user##";
																				template.Subject = "Test email ##param##";

																				mailTemplateAdded = template.Save();

																				var userValues = new Dictionary<string, object>
			                                                               		                 	{
			                                                               		                 		{"param", "blah"},
			                                                               		                 		{"param1", "blah1"},
			                                                               		                 		{"link", new LinkValue("http://localhost:777/", "Documents")},
			                                                               		                 		{"user", new UserValue(new Identifier(1), "Igor Solomatov")}
			                                                               		                 	};

																				mailFormatted = entityScope.QueueItem(TestTemplateID, "i.solomatov@mail.ru", userValues);


																				for (int i = 0; i < 10; i++)
																				{
																					var m = notificationInstanceFacade.Create();

																					m.Address = "i.solomatov@mail.ru";
																					m.Body = "Direct Added Body " + i;
																					m.Subject = "Direct Added Body";
																					m.SendAt = DateTime.Now.AddHours(-1);
																					m.RetriesNo = 0;

																					m.Save();
																				}

																				for (int i = 0; i < 10; i++)
																				{
																					var m1 = notificationInstanceFacade.Create();

																					m1.Address = "i.solomatov@mail.ru";
																					m1.Body = "Direct Added Body " + i;
																					m1.Subject = "Direct Added Body";
																					m1.SendAt = DateTime.Now.AddHours(-1);
																					m1.RetriesNo = 0;

																					m1.Save();
																				}

																				for (int i = 0; i < 10; i++)
																				{
																					var m1 = notificationInstanceFacade.Create();

																					m1.Address = "i.solomatov@mail.ru";
																					m1.Body = "Direct Added Body " + i;
																					m1.Subject = "Direct Added Body";
																					m1.SendAt = DateTime.Now.AddHours(1);
																					m1.RetriesNo = 0;

																					m1.Save();
																				}
																			});

			Assert.AreNotEqual(entityScope, null, "SPH.RunUnder completed with errors!");
			Assert.AreNotEqual(notificationInstanceFacade, null, "SPH.RunUnder completed with errors!");
			Assert.AreNotEqual(notificationTemplateFacade, null, "SPH.RunUnder completed with errors!");
			Assert.AreNotEqual(mailInstanceAdded, false, "SPH.RunUnder completed with errors!");
			Assert.AreNotEqual(mailTemplateAdded, false, "SPH.RunUnder completed with errors!");
			Assert.AreNotEqual(mailFormatted, false, "SPH.RunUnder completed with errors!");
		}
	}
}